package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dto.AdminLoginDTO;
import com.example.health_life.dto.UsersDTO;
import com.example.health_life.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //获取用户信息列表（需添加管理员可见的用户全部信息）
    @GetMapping("/users")
    public Response<List<UsersDTO>> getUsersList(@RequestParam(required = false) Integer status){
     List<UsersDTO> usersList = null;

    if (status == null || status == 0) {
        usersList  = userService.getUsersList(1); // 获取可用用户
    } else if (status == 1) {
        usersList = userService.getUsersList(0); // 获取全部用户
    }
        return Response.newSuccess(usersList);
}


    @GetMapping("/users/{id}")
    public Response<UsersDTO> getUsersById(@PathVariable int id){
        return Response.newSuccess(userService.getUserById(id));
    }

    @PostMapping("/users")
    public Response<Integer> addUsers(@RequestBody UsersDTO usersDTO){
        return Response.newSuccess(userService.addUsers(usersDTO));
    }

    @DeleteMapping("/users/{id}")//删除/激活用户（将用户的status更改为-1/1）
    public Response<Integer> deleteUsers(@PathVariable int id){
        return Response.newSuccess(userService.deleteUsers(id));
    }

    @PutMapping("/users/{id}")//更新用户信息(无法更改用户身份)
    public Response<UsersDTO> updateUsersById(@PathVariable int id, 
                                            @RequestParam(required = false) String username,
                                            @RequestParam(required = false) String nickname,
                                            @RequestParam(required = false) String avatar,
                                            @RequestParam(required = false) String gender,
                                            @RequestParam(required = false) String address,
                                            @RequestParam(required = false) String phone) {
        return Response.newSuccess(userService.updateUsersById(id, username, nickname, avatar, gender, address, phone));
    }

    @PutMapping("/users/{id}/role")//更新用户角色（管理员操作）
    public Response<UsersDTO> updateUserRole(@PathVariable int id, @RequestParam Byte role) {
        return Response.newSuccess(userService.updateUserRole(id, role));
    }

    /**
     * 微信小程序登录
//     * @param code 微信登录临时凭证
     * @return 用户信息
     */
    @PostMapping("/users/wx-login")
    public Response<UsersDTO> loginByWechat(@RequestBody Map<String, String> params) {
        UsersDTO dto = userService.loginByWechat(params.get("code"));
        System.out.println("返回DTO token: " + dto.getToken());
        return Response.newSuccess(dto);
    }

    /**
     * 绑定微信账号
     * @param userId 用户ID
     * @param code 微信登录临时凭证
     * @return 用户信息
     */
    @PostMapping("/users/{userId}/bind-wechat")
    public Response<UsersDTO> bindWechatAccount(@PathVariable Integer userId, @RequestParam String code) {
        return Response.newSuccess(userService.bindWechatAccount(userId, code));
    }

    /**
     * 解绑微信账号
     * @param userId 用户ID
     * @return 用户信息
     */
    @PostMapping("/users/{userId}/unbind-wechat")
    public Response<UsersDTO> unbindWechatAccount(@PathVariable Integer userId) {
        return Response.newSuccess(userService.unbindWechatAccount(userId));
    }

    /**
     * 根据openid获取用户信息
     * @param openid 微信openid
     * @return 用户信息
     */
    @GetMapping("/users/wx/{openid}")
    public Response<UsersDTO> getUserByOpenid(@PathVariable String openid) {
        return Response.newSuccess(userService.getUserByOpenid(openid));
    }

    @PutMapping("/users/{id}/status")
    public Response<UsersDTO> updateUserStatus(@PathVariable int id, @RequestParam Byte status) {
        return Response.newSuccess(userService.updateUserStatus(id, status));
    }

    /**
     * 管理员登录
     * @param loginDTO 登录信息（用户名和密码）
     * @return 用户信息（包含token）
     */
    @PostMapping("/admin/login")
    public Response<UsersDTO> adminLogin(@RequestBody AdminLoginDTO loginDTO) {
        try {
            UsersDTO dto = userService.adminLogin(loginDTO.getUsername(), loginDTO.getPassword());
            return Response.newSuccess(dto);
        } catch (IllegalStateException e) {
            return Response.newFail(e.getMessage());
        }
    }
}
